<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/4
 * Time: 9:35
 */

namespace App;

use App\BaseDictionary;
use Illuminate\Database\Eloquent\Model;

class UserActivityRequests extends Model
{
    protected $table = 'user_activity_requests';
    public $timestamps = false;

    public static $ACTIVITY_TYPE_COMMUNITY = 1;
    public static $ACTIVITY_TYPE_OLDER = 2;
    public static $ACTIVITY_TYPE_GROUP = 3;
    public static $ACTIVITY_TYPE_PARTY = 4;

    public static $ACTIVITY_TYPE_MAP = [
        ['key' => 1, 'text' => '社区任务'],
        ['key' => 2, 'text' => '老人任务'],
        ['key' => 3, 'text' => '团队任务'],
        ['key' => 4, 'text' => '党建活动']
    ];

    public static $USER_APPLY_ACTIVITY_STATUS_PASS = 1;
    public static $USER_APPLY_ACTIVITY_STATUS_DENY = 0;

    public static $USER_APPLY_ACTIVITY_STATUS_MAP = [
        ['key' => 1, 'text' => '已通过'],
        ['key' => 0, 'text' => '已拒绝'],
    ];
    protected $fillable = [
        'user_id', 'activity_id', 'city_id', 'request_time', 'process_time', 'process_result', 'deny_reason'
    ];

    public function relateCity()
    {
        return $this->belongsTo('App\City', 'city_id', 'id');
    }

    public function cities()
    {
        return $this->belongsTo('App\City', 'city_id', 'id')->first();
    }

    public function relateActivity()
    {
        return $this->belongsTo('App\Activity', 'activity_id', 'id');
    }

    public function activities()
    {
        return $this->belongsTo('App\Activity', 'activity_id', 'id')->first();
    }

    public function relateUser()
    {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }

    public function users()
    {
        return $this->belongsTo('App\User', 'user_id', 'id')->first();
    }

    public function relateMajorGroup()
    {
        return $this->belongsToMany('App\Activity','activity_groups', 'activity_id', 'group_id');
    }

    public function majorGroup()
    {
        return $this->activities()->belongsToMany('App\UserGroup', 'activity_groups', 'activity_id', 'group_id')
            ->wherePivot('major_flag', BaseDictionary::$KEY_YES)->first();
    }


}